Skip to content

Fix: Duplicate File Parts Generated in MockMvc using MockPart #954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

Lee-WonJun
Copy link

@Lee-WonJun Lee-WonJun commented Jan 6, 2025

PR for Issue #953

Solution:

  1. Compare if the File and Part contain the same content. If they do, remove one of them (assuming they are paired, so once removed, it will not be compared again).
  2. If they are not the same, add the part to the parts list.
  3. The MockPart implementation itself calls setContentDispositionFormData, which is only added to Part. Therefore, when comparing, setContentDispositionFormData is also included in the comparison.

Reference Code (MockPart):

public MockPart(String name, @Nullable String filename, @Nullable byte[] content, @Nullable MediaType contentType) {
		Assert.hasLength(name, "'name' must not be empty");
		this.name = name;
		this.filename = filename;
		this.content = (content != null ? content : new byte[0]);
		this.headers.setContentDispositionFormData(name, filename);
		this.headers.setContentType(contentType);
	}

Additional Information:

I initially wrote a test code to compare the MediaType content type, but due to compatibility checks, I could not use the MockPart constructor that allows defining contentType (introduced in @since 6.1.2). As a result, I removed it from the test.

@Lee-WonJun Lee-WonJun changed the title Fix: Duplicate File Parts Generated in MockMultipartHttpServletRequest Fix: Duplicate File Parts Generated in MockMvc using MockPart Jan 6, 2025
@wilkinsona
Copy link
Member

Thanks for the PR, but it's too early to consider possible fixes for #953. It's not yet clear if a change in REST Docs is even the right thing to do.

@wilkinsona wilkinsona closed this Jan 6, 2025
@wilkinsona wilkinsona added status: declined Suggestion or change that we don't want to make at this time and removed status: waiting-for-triage Untriaged issue labels Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined Suggestion or change that we don't want to make at this time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants